
#import "PUFoodXuanVC.h"
#import "PUXuanCaiVC.h"
#import "PURecordVC.h"
#import "PUPublishVC.h"
#import "PUPhoneLogin.h"
#import "JXCategoryView.h"

@interface PUFoodXuanVC ()<JXCategoryViewDelegate, JXCategoryListContainerViewDelegate>

@property (nonatomic, strong) UIScrollView          *scrollView;
@property (nonatomic, strong) NSArray               *titles;
@property (nonatomic, strong) NSMutableArray        *childVCs;
@property (nonatomic, strong) JXCategoryTitleView   *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic, strong) NSArray *appPages;

@end

@implementation PUFoodXuanVC

- (UIView *)listView {
    return self.view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    !self.scrollCallback ?: self.scrollCallback(scrollView);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = UIColor.whiteColor;
    
    UIImageView *img=[[UIImageView alloc]init];
    img.backgroundColor=[UIColor whiteColor];
    img.image=[UIImage imageNamed:@"我的背景"];
    [self.view addSubview:img];
    
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(0);
       
    }];
    
    UIButton *btn=[[UIButton alloc]init];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"发布"] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(56*KWidthScale);
        make.right.equalTo(self.view.mas_right).offset(-12*KWidthScale);
        make.width.equalTo(@(28*KWidthScale));
        make.height.equalTo(@(28*KWidthScale));
    }];
    
    PUXuanCaiVC *vc1=[[PUXuanCaiVC alloc]init];
    
    PURecordVC *vc2=[[PURecordVC alloc]init];
    self.appPages = @[vc1,vc2];
    
    [self.view addSubview:self.categoryView];
    [self.view addSubview:self.listContainerView];
    self.listContainerView.backgroundColor=[UIColor clearColor];

    self.categoryView.frame = CGRectMake(-15, [self tz_safeAreaInsets].top+6, 150, 44);
    self.listContainerView.frame = CGRectMake(0, [self tz_safeAreaInsets].top + 44+10+6, _window_width, _window_height - [self tz_safeAreaInsets].top - 44 - [self tz_safeAreaInsets].bottom - 49-16);
    [self.listContainerView reloadData];
    [self.categoryView reloadData];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushGHJLNotification) name:@"pushGHJLNotification" object:nil];
    
}

-(void)pushGHJLNotification{
    [self.categoryView selectItemAtIndex:1];
}

-(void)btnClick{
    if ([Config getToken]==nil) {
        [self noLogin];
        return;
    }
    
    PUPublishVC *vc=[[PUPublishVC alloc]init];
    vc.type=1;
    vc.huaTiStr=@"";
//    vc.modalPresentationStyle=0;
//    [self presentViewController:vc animated:YES completion:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)noLogin{
    PUPhoneLogin *vc=[[PUPhoneLogin alloc]init];
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
    navVc.modalPresentationStyle=0;
    [self presentViewController:navVc animated:NO completion:nil];
}

- (UIEdgeInsets)tz_safeAreaInsets {
    UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
    if (![window isKeyWindow]) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        if (CGRectEqualToRect(keyWindow.bounds, [UIScreen mainScreen].bounds)) {
            window = keyWindow;
        }
    }
    if (@available(iOS 11.0, *)) {
        UIEdgeInsets insets = [window safeAreaInsets];
        return insets;
    }
    return UIEdgeInsetsZero;
}

#pragma mark - LateLoveAppJXViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
}

#pragma mark - LateLoveAppJXListContainerViewDelegate
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.categoryView.titles.count;
}

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {

    return self.appPages[index];
}


#pragma mark - lazy UI
- (JXCategoryTitleView *)categoryView {
    if (!_categoryView) {
        _categoryView = [[JXCategoryTitleView alloc] init];
        _categoryView.defaultSelectedIndex = 0;
        _categoryView.delegate = self;
        _categoryView.titles = @[@"热门", @"记录"];
        

        _categoryView.titleFont = [UIFont systemFontOfSize:16];
        _categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
        _categoryView.titleColor = [UIColor colorWithHex:0x333333];
        _categoryView.titleSelectedColor = [UIColor colorWithHex:0x333333];
        _categoryView.titleLabelAnchorPointStyle = JXCategoryTitleLabelAnchorPointStyleBottom;
        JXCategoryIndicatorLineView *appLineView = [[JXCategoryIndicatorLineView alloc] init];
        appLineView.indicatorWidth = 20;
        appLineView.indicatorHeight = 5;
        appLineView.indicatorColor = [UIColor colorWithHex:0xFF729A];
        appLineView.verticalMargin = 3;
        _categoryView.indicators = @[appLineView];
        
        _categoryView.listContainer = self.listContainerView;
    }
    return _categoryView;
}

- (JXCategoryListContainerView *)listContainerView {
    if (!_listContainerView) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    }
    return _listContainerView;
}

@end
